package com.yun.baymax.activity;

import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.yun.lib.ui.CustomTitleBar;
import com.yun.baymax.R;
import com.yun.baymax.bean.eventbus.EmergencyMessage;

public class CallActivity extends AppCompatActivity {

    private MediaPlayer mp;
    private ImageView fab;
    private CustomTitleBar titleBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置点亮屏幕
        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                        | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                        | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                        | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
        );

        setContentView(R.layout.activity_call);
        //隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        fab = (ImageView) findViewById(R.id.fab);
        titleBar = (CustomTitleBar) findViewById(R.id.title);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();*/
                mp.stop();
                fab.setVisibility(View.INVISIBLE);
                finish();

            }
        });

        onMessageEvent(null);
    }

    private void playRing() {
        mp = new MediaPlayer();
        try {
            mp.setDataSource(this, RingtoneManager
                    .getDefaultUri(RingtoneManager.TYPE_RINGTONE));
            mp.prepare();
            mp.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        playRing();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mp.stop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void onMessageEvent(EmergencyMessage messageReceive) {
        String title = getIntent().getStringExtra("username");
//        playRing();
        fab.setVisibility(View.VISIBLE);
//        ChatUser chatUser = ChatUserCache.getInstance().getChatUser(title);
//        titleBar.setTitle(chatUser.getNick());
    }
}
